#pragma once

#include<iostream>
#include<string>
#include<sys/socket.h>
#include<netinet/in.h> //struct sock_addr_in
#include<sys/un.h> //struct sock_addr_un
#include<strings.h> // for bzero
#include<netinet/in.h> //for sock_addr_in
#include<arpa/inet.h> // for inet_addr

#include"Log.hpp"
using namespace LogModule;

class UdpEcho
{
public:
    UdpEcho(std::string ip , int port)
        :_ip(ip)
        ,_port(port)
    {}
    void Init()
    {
        _sock_id = socket(AF_INET,SOCK_DGRAM,0); //IPV4协议(网络层) , UDP协议(传输层)

        struct sockaddr_in local;
        bzero(&local,sizeof(local));

        local.sin_family = AF_INET;
        local.sin_port = htonl(_port);
        local.sin_addr.s_addr = inet_addr(_ip.c_str());

        if(bind(_sock_id,(struct sockaddr*)&local,sizeof(local)))
        {
            LOG(LogLevel::INFO) << "bind fail!!!\n";
            exit(1);
        }
        else
        {
            LOG(LogLevel::INFO) << "bind success!!!\n" ;
        }
    }
    ~UdpEcho()
    {}
private:
    int _sock_id;
    int _port;
    std::string _ip;

};